// pages/bridalParty/bridalParty.js
const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
Page({
  data: {
    relationList:[],  //家庭关系列表
    bridalList: [],  //亲友团列表
    loginUser: false,//当前登陆人
    token: '',
    babyList: [],
    changeBabyData: {},
    addRelationData: {},
    ageStr: '',
    currentIndex: 0,
    relationIndex: 0,
    headerImg: '',
    lock:true,
  },
  onLoad: function (options) {
    let that = this;
    //获取token数据
    let img = wx.getStorageSync("userInfo").avatar
    this.setData({
      token: wx.getStorageSync('token'),
      headerImg:img
    })
    //获取宝宝列表
    this.getBabyList();
  },
  onShow(){
   
  },
  //获取宝宝列表
  getBabyList() {
    utils.HttpRequst(true,"/baby-cloud-base/mobileBabyInfo/listAll","post",{},(res)=> {
      console.log(res)
      if (res.data.code == 200 && res.data.data.length > 0) {
        this.setData({
          babyList: res.data.data,
          changeBabyData: res.data.data[0],
          ageStr: utils.getBirthSlot(res.data.data[0].birthday.split(" ")[0])
        })
        //默认请求第一个宝宝的亲友团列表   参数宝宝id
        this.getbridalList(this.data.babyList[0].id)
      }
    });
  },
  changeBabyHeader(e) {
    this.setData({
      currentIndex: e.target.dataset.idx,
      changeBabyData: this.data.babyList[e.target.dataset.idx],
      ageStr: utils.getBirthSlot(this.data.babyList[e.target.dataset.idx].birthday.split(" ")[0]),
      relationIndex: this.data.bridalList,//获取切换宝宝后的亲友团列表
    })
    this.getbridalList(this.data.changeBabyData.id)
  },
  //获取亲友团列表
  getbridalList(babyId) {
    utils.HttpRequst(true,"/baby-cloud-base/mobileBabyRelation/list","post",{babyId: babyId},(res)=> {
      if (res.data.code == 200) {
        var _bridalList = res.data.data
        _bridalList.forEach(item=>{
          item.comeTime = item.comeTime.substring(0,16)
        })
        this.setData({
          bridalList: _bridalList,
        })
        console.log(_bridalList)
        //获取家庭关系列表
        this.getrelationList();
      }
    })
  },
  //获取关系列表
  getrelationList() {
    let dict = wx.getStorageSync("dict");
    let babyrelation = dict["BABYRELATION"];
    var _relationList = babyrelation.data;
    let bridalList = this.data.bridalList;
    let data = this.handlebridalList(_relationList, bridalList)
    this.setData({
      relationList: data,
    });
  },
  handlebridalList(a,b){
    let arr = [];
    b.forEach(item=>{
      a.forEach((prop,index)=>{
        if (item.userCallCode == prop.value){
          a.splice(index, 1);
        }
      })
    })
    return a;
  },
  //新增亲友团
  addBridalParty(event) {
    let that = this;
    if (that.data.lock){
    that.setData({
      lock:false
    })
    let addBridalData = event.currentTarget.dataset;
    let babyId = this.data.changeBabyData.id;
    let userCallName = addBridalData.name;
    let userCallCode = addBridalData.value;
    utils.HttpRequst(
      true, 
      "/baby-cloud-base/mobileBabyRelation/addItem",
      "post",
      {
        babyId,
        userCallName,
        userCallCode
      },
      (res)=> {
      console.log(res)
      if (res.data.code == 200) {
        that.getbridalList(babyId)
        that.setData({
          lock:true
        })
        
      }
    });
    }
  },
  onShareAppMessage(e){
    console.log(e)
    //转发
    let clickIndex = e.target.dataset.index;
    let id = this.data.bridalList[clickIndex].id;
    let currentBaby = this.data.changeBabyData;
    return {
      title: `${currentBaby.name}宝宝邀请您加入亲友团，陪伴并见证他/她成长的每一天。`,
      path: `/pages/index/index?relationId=${id}`, //这里拼接需要携带的参数
      imageUrl: '/images/appBanner.png',
      success: function (res) {
        console.log("转发成功" + res);
      }
    }
  }
})